import { getCategoryAllList } from '@/api/goods/category';
import { ref } from 'vue';

const modelMixin = {
    setup() {
        const dicts = ref([]); // 字典数据
        const form = ref({});
        const loading = ref(false);

        const edit = (id) => {
            this.id = id;
            this.open = true;
            this.getDetail(id);
        };

        const getAllDicts = () => {
            this.getDicts(this.dicts.join(',')).then((res) => {
                console.log('res :>> ', res);
            });
        };

        const getDetail = (row) => {
            loading.value = true;
            this.funcObj.get(row.id)
                .then((res) => {
                    loading.value = false;
                    if (res.code == 200) {
                        form.value = res.data;
                        if (typeof this.detailAfter === 'function') {
                            this.detailAfter();
                        }
                    } else {
                        this.$modal.msgError(res.msg);
                    }
                })
                .catch((err) => {
                    loading.value = false;
                    console.log('err :>> ', err);
                });
        };

        const submitForm = (close = true) => {
            console.log(666);
            this.$refs.form.validate((valid) => {
                if (valid) {
                    loading.value = true;
                    typeof this.classifyForm === 'function' && this.classifyForm();
                    console.log('this.form :>> ', form.value);
                    if (form.value.id) {
                        this.funcObj.update(form.value)
                            .then((res) => {
                                loading.value = false;
                                if (res.code == 200) {
                                    this.$modal.msgSuccess('编辑成功')
                                    this.open = false;
                                    this.$emit('ok');
                                } else {
                                    this.$modal.msgError(res.msg);
                                }
                            })
                            .catch((err) => {
                                loading.value = false;
                                console.log('err :>> ', err);
                            });
                    } else {
                        this.funcObj.add(form.value)
                            .then((res) => {
                                loading.value = true;
                                if (res.code == 200) {
                                    this.$modal.msgSuccess('新增成功')
                                    this.open = false;
                                    this.$emit('ok');
                                } else {
                                    this.$modal.msgError(res.msg);
                                }
                            })
                            .catch((err) => {
                                console.log('err :>> ', err);
                                loading.value = false;
                            });
                    }
                }
            });
        };

        const cancel = () => {
            this.open = false;
            this.$emit('cancel');
        };

        const getCategoryAllList = () => {
            getCategoryAllList().then((res) => {
                if (res.code == 200) {
                    this.categoryList = res.data;
                } else {
                    this.$modal.msgError(res.msg);
                }
            });
        };

        return {
            dicts,
            form,
            loading,
            edit,
            getAllDicts,
            getDetail,
            submitForm,
            cancel,
            getCategoryAllList,
        };
    },
};

export default modelMixin;
